package com.project.andeper.paperproject.app;

import android.app.Application;
import android.content.Context;
import android.util.Log;
import android.widget.Toast;

import com.project.andeper.paperproject.DBManager;
import com.wyk.greendaodemo.greendao.gen.HourData;
import com.wyk.greendaodemo.greendao.gen.MonthData;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class App extends Application {

    private static Context context;
    //application实例
    private static App instance;
    DBManager dbManger;
    @Override
    public void onCreate() {
        super.onCreate();
        context = this;
        instance = this;
        //dbManger = new DBManager(context);
        dbManger = DBManager.getInstance(context);
        List<MonthData> monthDatas = dbManger.queryMonthData();
        List<HourData> hourDatas = dbManger.queryHourData();
        if(monthDatas.size() == 0) {
            initData();
        }
        if(hourDatas.size() == 0) {
            initDataHour();
        }


        /*HourData hourData= new HourData();
        //getCell(x,y)表示第y行的第x列
        hourData.setOrderNum(1);
        hourData.setNumber("05-131-15");
        hourData.setModel("门坐起重机");
        hourData.setConsumption(4.626944444);
        hourData.setHour(1);
        dbManger.insertHourData(hourData);*/

    }

    private void initData(){
        try {
            InputStream inputReader = getResources().getAssets().open("data.xls");
            Workbook book = null;
            book = Workbook.getWorkbook(inputReader);
            Sheet sheet = book.getSheet(0);
            int Rows = sheet.getRows();
            int Cols = sheet.getColumns();
            ArrayList<MonthData> monthDatas = new ArrayList<>();
            for (int i = 0; i < Rows; ++i) {
                MonthData monthData = new MonthData();
                monthData.setOrderNum(Integer.parseInt(sheet.getCell(0,i).getContents()));
                monthData.setNumber(sheet.getCell(1,i).getContents());
                monthData.setMachine(sheet.getCell(2,i).getContents());
                monthData.setModel(sheet.getCell(3,i).getContents());
                monthData.setTable(sheet.getCell(4,i).getContents());
                monthData.setVariety(sheet.getCell(5,i).getContents());
                monthData.setUseFor(sheet.getCell(6,i).getContents());
                monthData.setDepartment(sheet.getCell(7,i).getContents());
                monthData.setUseType(sheet.getCell(8,i).getContents());
                monthData.setSerialNumber(Integer.parseInt(sheet.getCell(9,i).getContents()));
                monthData.setEquipmentNumber(sheet.getCell(10,i).getContents());
                monthData.setEquipmentName(sheet.getCell(11,i).getContents());
                monthData.setPower(Integer.parseInt(sheet.getCell(12,i).getContents()));
                monthData.setTableNum(sheet.getCell(13,i).getContents());
                monthData.setMileage(Double.parseDouble(sheet.getCell(14,i).getContents()));
                monthData.setMonth(Integer.parseInt(sheet.getCell(15,i).getContents()));
                monthDatas.add(monthData);
            }
            dbManger.insertMonthDataList(monthDatas);

        }catch (IOException e){
            e.printStackTrace();
        }catch (BiffException b){
            b.printStackTrace();
        }

    }

    private void initDataHour(){
        try{
            Log.e("FINDD","1");
            InputStream inputReader = getResources().getAssets().open("hourData.xls");
            Log.e("FINDD","2");
            Workbook book = null;
            Log.e("FINDD","3");
            book = Workbook.getWorkbook(inputReader);
            Log.e("FINDD","4");
            Sheet sheet = book.getSheet(0);
            Log.e("FINDD","5");
            int Rows = sheet.getRows();
            Log.e("FINDD","6");
            int Cols = sheet.getColumns();
            Log.e("FINDD","7");
            List<HourData> hourDatas = new ArrayList<>();
            for (int i = 0; i < Rows; ++i){
                HourData hourData= new HourData();
                //getCell(x,y)表示第y行的第x列
                hourData.setOrderNum(Integer.parseInt(sheet.getCell(0,i).getContents()));
                hourData.setNumber(sheet.getCell(1,i).getContents());
                hourData.setModel(sheet.getCell(2,i).getContents());
                hourData.setConsumption(Double.parseDouble(sheet.getCell(3,i).getContents()));
                hourData.setHour(Integer.parseInt(sheet.getCell(4,i).getContents()));
                hourDatas.add(hourData);
            }
            Log.e("FINDD","真正开始");
            dbManger.insertHourDataList(hourDatas);
            Log.e("FINDD","结束");
        } catch (IOException e) {
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
            Toast.makeText(this,"出错啦！",Toast.LENGTH_SHORT).show();
        }
    }

    public static Context getContext() {
        return context;
    }

    public static App getInstance() {
        return instance;
    }
}
